﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Net;
using System.Xml.Serialization;
using FinMkts.CFD.Rest;
using Microsoft.Practices.Composite.Events;

namespace FinMkts.CFD.DataService
{
    [Export(typeof(ICfdService))]
    public class CfdService : ICfdService
    {
        private readonly IEventAggregator _eventAggregator;

        [ImportingConstructor]
        public CfdService(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
        }

        public void GetMarketPlaces()
        {
            var webRequest = WebRequest.Create("http://localhost/FinMkts.CFD.Rest/CFD/") as HttpWebRequest;
            if (webRequest != null)
                webRequest.BeginGetResponse(GetMarketPlacesResult, webRequest);
        }

        private void GetMarketPlacesResult(IAsyncResult ar)
        {
            var webRequest = ar.AsyncState as HttpWebRequest;
            if (webRequest == null) return;

            WebResponse webResponse = webRequest.EndGetResponse(ar);

            var s = new XmlSerializer(typeof(List<MarketPlaceDC>));
            var marketPlaces = (List<MarketPlaceDC>)s.Deserialize(webResponse.GetResponseStream());

            _eventAggregator.GetEvent<MarketPlacesReceivedEvent>().Publish(new MarketPlacesEventArgs(marketPlaces));
        }

        public void GetMarkets(string marketPlace)
        {
            var webRequest = WebRequest.Create(string.Format("http://localhost/FinMkts.CFD.Rest/CFD/{0}/", marketPlace)) as HttpWebRequest;
            if (webRequest != null)
                webRequest.BeginGetResponse(GetMarketsResult, webRequest);
        }

        private void GetMarketsResult(IAsyncResult ar)
        {
            var webRequest = ar.AsyncState as HttpWebRequest;
            if (webRequest == null) return;

            WebResponse webResponse = webRequest.EndGetResponse(ar);

            var s = new XmlSerializer(typeof(List<CfdMarketDC>));
            var cfdMarkets = (List<CfdMarketDC>)s.Deserialize(webResponse.GetResponseStream());

            _eventAggregator.GetEvent<CfdMarketsReceivedEvent>().Publish(new CfdMarketsEventArgs(cfdMarkets));
        }

        public void Get(string marketPlace, string listingCode)
        {
            var webRequest = WebRequest.Create(string.Format("http://localhost/FinMkts.CFD.Rest/CFD/{0}/{1}/", marketPlace, listingCode)) as HttpWebRequest;
            if (webRequest != null)
                webRequest.BeginGetResponse(GetResult, webRequest);
        }

        private void GetResult(IAsyncResult ar)
        {
            var webRequest = ar.AsyncState as HttpWebRequest;
            if (webRequest == null) return;

            WebResponse webResponse = webRequest.EndGetResponse(ar);

            var s = new XmlSerializer(typeof(CfdMarketDC));
            var cfdMarketDc = (CfdMarketDC)s.Deserialize(webResponse.GetResponseStream());

            _eventAggregator.GetEvent<CfdMarketReceivedEvent>().Publish(new CfdMarketEventArgs(cfdMarketDc));
        }
    }
}
